/**
 * Copyright 2011 Enjoy Change, Inc. All rights reserved.
 * Use is subject to license terms.
 */
package com.enjoychange.enjoy.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.enjoychange.framework.component.storycase.StoryCase;
import com.enjoychange.framework.component.storycase.application.IStoryCaseService;



/**
 * @author l90003709
 *
 */
@Controller
public class StoryController {
	@Autowired
	IStoryCaseService storyCaseService;
	
	@RequestMapping("/listStoryCase.do")
	public String list(HttpServletRequest request) {
		Map<String, Object> paras=new HashMap<String, Object>();
		if(StringUtils.isNotBlank(request.getParameter("storyid"))){
		    paras.put("storyid", request.getParameter("storyid"));
		}
			
		List<StoryCase> storyCaseList=storyCaseService.queryStoryCase(paras);
		request.setAttribute("storyCaseList", storyCaseList);
		return "storycase/list";
	}

	@RequestMapping("/saveStoryCase.do")
	public String save(HttpServletRequest request,HttpServletResponse response,StoryCase storyCase) throws IOException {
		boolean isNewStoryCase=StringUtils.isNotBlank(request.getParameter("isCreateStoryCase"));
		storyCaseService.saveStoryCase(storyCase,isNewStoryCase);		
	    response.sendRedirect("listStoryCase.do");
	
	    return "storycase/list";
	}
	@RequestMapping("/deleteStoryCase.do")
	public String delete(HttpServletRequest request,HttpServletResponse response) throws IOException {
		String storyid=request.getParameter("storyid");			
		storyCaseService.deleteStoryCase(storyid);
		response.sendRedirect("listStoryCase.do");
		return "storycase/list";
	}
	@RequestMapping("/editStoryCase.do")
	public String edit(HttpServletRequest request) {
		boolean isCreateStoryCase=StringUtils.isNotBlank(request.getParameter("isCreateStoryCase"));
		//仅仅跳转到detai页面，无需查询
		if(isCreateStoryCase){
			request.setAttribute("isCreateStoryCase", true);
			return "storycase/detail";
		}
		Map<String, Object> paras=new HashMap<String, Object>();
		if(StringUtils.isNotBlank(request.getParameter("storyid"))){
		    paras.put("storyid", request.getParameter("storyid"));
		}
		//有且只有一个返回对象	
		List<StoryCase> storyCaseList=storyCaseService.queryStoryCase(paras);
		request.setAttribute("storyCase", storyCaseList.get(0));
		return "storycase/detail";
	}
}
